<?php
namespace Biz\model\trade;

use Doraemon\pockets\datebase\BaseDbModel;

class OrderMatching extends BaseDbModel
{
	const _CONFIG_FILE_ = 'db_shop';

	const _CONFIG_SELECT_ = 'db_trade';

	const _DATABASE_ = 'shop_trade';

	const _TABLE_NAME = 'order_matching';

	const CONFIRM_FIELD_GOODS_TITLE = 'goods_title';
	const CONFIRM_FIELD_CONSIGNEE_TIME = 'consignee_time';
	const CONFIRM_FIELD_CONSIGNEE_ADDRESS = 'consignee_address';
	const CONFIRM_FIELD_CONSIGNEE_PHONE = 'consignee_phone';
	const CONFIRM_FIELD_CONSIGNEE = 'consignee';
	const CONFIRM_FIELD_IS_INTER_COMMUNITY = 'is_inter_community';
	const CONFIRM_FIELD_IS_HAS_ELEVATOR = 'is_has_elevator';
	const CONFIRM_FIELD_UPSTAIR_COST = 'upstair_cost';

	const CONFIRM_FIELD_MAPS = [
		self::CONFIRM_FIELD_GOODS_TITLE => '产品',
		self::CONFIRM_FIELD_CONSIGNEE_TIME => '期望送货时间',
		self::CONFIRM_FIELD_CONSIGNEE_ADDRESS => '收货地址',
		self::CONFIRM_FIELD_CONSIGNEE_PHONE => '收货电话',
		self::CONFIRM_FIELD_CONSIGNEE => '收货人',
		self::CONFIRM_FIELD_IS_INTER_COMMUNITY => '小区能否进入',
		self::CONFIRM_FIELD_IS_HAS_ELEVATOR => '是否有电梯',
		self::CONFIRM_FIELD_UPSTAIR_COST => '预估上楼费用',
	];

	//是否可以进小区
	const CAN_ENTER_COMMUNITY_YES = 1;//可以
    const CAN_ENTER_COMMUNITY_NO = 0;//不可以

    //是否有电梯
    const HAVE_ELEVATOR_YES = 1;//有
    const HAVE_ELEVATOR_NO = 0;//无

	public static function getInfo($orderNo)
	{
		return self::select('*', ['order_no' => $orderNo]);
	}

	public static function addConfirmInfo($orderNo,$data)
	{
		$id = self::get('id', ['order_no' => $orderNo]);
		if (!$id) {
			$data['order_no'] = $orderNo;
			self::insert($data);
			return self::id();
		} else {
			self::update($data,['id'=>$id]);
			return $id;
		}
	}

	public static function getOneInfo($orderNo){
        $info = self::get('*', ['order_no' => $orderNo]);

        self::formatInfo($info);

        return $info;

    }

    protected static function formatInfo(&$info){
	    $canEnterCommunity = '是否可以进小区(未设置)';
        $haveElevator = '是否有电梯(未设置)';
        $whichFloor = '几楼(未设置)';

        if(!empty($info)){
            foreach ($info as $field=>$value){
                switch ($field){
                    case 'is_inter_community':
                        $canEnterCommunity = ($value == self::CAN_ENTER_COMMUNITY_YES) ? '物流车可以进小区':'物流车不可以进小区';
                        break;
                    case 'is_has_elevator':
                        $haveElevator = ($value == self::HAVE_ELEVATOR_YES)?'有电梯':'无电梯';
                        break;
                    case 'which_floor':
                        $whichFloor = ($value != 0)?"$value 楼":'';
                }
            }
        }



        $info['receipt_tipinfo_desc'] = sprintf("%s %s %s",$canEnterCommunity,$haveElevator,$whichFloor);
    }

}